package net.ghy.statistics.utils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.bat.db.BaseDAOImpl;
import net.ghy.BXService;
import net.ghy.statistics.department.DepExpData;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import dao.hb.BxMidle;
import dao.hb.UUser;
/* statistics helper 
 * 0307 fucj
 * */

public class EUtils {
	// 创建ApplicationContext
	private static ApplicationContext ctx = null;
	private static List uidList = null;
	private static BaseDAOImpl dao = null;
	static public ApplicationContext getCtx(){
		if(ctx==null){
			String[] locations = {"applicationContext-common.xml", "applicationContext-beans.xml"};
			ctx = new ClassPathXmlApplicationContext(locations ); 	
		}
		return ctx;
	}
	//通过uid获取用户name
	public static UUser getUserbyid(String uid){
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		String hql =  " from UUser u where u.id=" + uid;
		List uList = dao.find(hql);
		UUser u = (UUser ) uList.get(0);
		return u;
	}
	
	
	//通用方法获取在职合计  需+日期
	public static Map<String, List> getStateTotaltj(String wkstate,String start,String end) {
		Map<String, BxMidle> wktj = new HashMap<String, BxMidle>();
		Map<String, List> wkStatetj = new HashMap<String, List>();
		List<Map> zj = new ArrayList<Map>();
		BxMidle mztj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		BxMidle zytj = new BxMidle(0.0, 0.0, 0.0, 0.0);
		String mzhql = "from BxMidle b where b.wkState='"
				+ wkstate + "'" + " and b.bxtype = '门诊' and ( b.dtLook >= '" +start + "' and b.dtLook <='" + end +"')";
		String zyhql = "from BxMidle b where b.wkState='"
				+ wkstate + "'" + " and b.bxtype = '住院' and ( b.dtLook >= '" +start + "' and b.dtLook <='" + end +"')";
		ctx = EUtils.getCtx();
		dao = (BaseDAOImpl) ctx.getBean("BaseDAOImpl");
		List mzbx = dao.find(mzhql);
		List zybx = dao.find(zyhql);
		double cpayone =  Math.round(zytj.getPayone()*100) , cpaytwo = Math.round(zytj.getPaytwo()*100), cpayall = Math.round( zytj.getPayall()*100), ctotal =  Math.round(zytj.getBxSum()*100);
		double mpayone = Math.round(mztj.getPayone()*100), mpaytwo = Math.round(mztj.getPaytwo()*100), mpayall =Math.round( mztj
				.getPayall()*100), mtotal =Math.round( mztj.getBxSum()*100);
		for (int i = 0; i < zybx.size(); i++) {
			BxMidle b = (BxMidle) zybx.get(i);
			cpayone += Math.round(b.getPayone()*100);
			cpaytwo += Math.round(b.getPaytwo()*100);
			cpayall += Math.round(b.getPayall()*100);
			ctotal += Math.round(b.getBxSum()*100);
		}
		zytj.setPayall(cpayall/100.0);
		zytj.setPayone(cpayone/100.0);
		zytj.setPaytwo(cpaytwo/100.0);
		zytj.setBxSum(ctotal/100.0);
		for (int i = 0; i < mzbx.size(); i++) {
			BxMidle b = (BxMidle) mzbx.get(i);
			mpayone += Math.round(b.getPayone()*100);
			mpaytwo += Math.round(b.getPaytwo()*100);
			mpayall += Math.round(b.getPayall()*100);
			mtotal +=Math.round( b.getBxSum()*100);
		}
		mztj.setPayall(mpayall/100.0);
		mztj.setPayone(mpayone/100.0);
		mztj.setPaytwo(mpaytwo/100.0);
		mztj.setBxSum(mtotal/100.0);
		wktj.put("住院", zytj);
		wktj.put("门诊", mztj);
		zj.add(wktj);
		wkStatetj.put(wkstate+"总计", zj);
		return wkStatetj;

	}
	//通用方法获取在职总扣除包干费   需+日期
	public static Map<String, List> getStateTotaltjBaogf(String wkstate,String start,String end) throws Exception {
		Map<String, List> wkState = new HashMap<String, List>();
		Map<String, List> wkStatetj = getStateTotaltj(wkstate,start,end);
		Map<String, BxMidle> bgftjm = new HashMap<String, BxMidle>();
		double baogf = 0.0;
		DepExpData ded = new DepExpData();
		if(wkstate.equals("在职")){
			if(uidList==null){
				uidList=EUtils.getUidList(start, end);
			}
			for(int i = 0 ; i< uidList.size();i++){
				baogf+=(Math.round(ded.getBaogf(uidList.get(i).toString(), start, end)*100)/100.0);
			}
		}
		List<Map> zj = wkStatetj.get(wkstate+"总计");
		BxMidle mztj = (BxMidle) zj.get(0).get("门诊");
		BxMidle zytj = (BxMidle) zj.get(0).get("住院");
		BxMidle bgftj = new BxMidle();
		double payall = Math.round(mztj.getBxSum()*100+zytj.getBxSum()*100)/100.0;
		double bxsum =Math.round( payall*100-baogf*100)/100.0;
		bgftj.setBxSum(bxsum);
		bgftj.setPayall(payall);
		bgftj.setPayone(baogf);
		bgftjm.put("包干费", bgftj);
		zj.add(bgftjm);
		wkState.put(wkstate+"总计", zj);
		return wkState;
	}
	
	
	//全院统计辅助方法
	public static List getUidList(String start,String end){
		ctx = EUtils.getCtx();
        dao = (BaseDAOImpl) ctx.getBean("BaseDAOImpl");
        String hql = "select distinct uid from BxMidle b where (b.bxtype='门诊 ' or b.bxtype='住院') and b.wkState='在职' and ( b.dtLook >= '" +start + "' and b.dtLook <='" + end +"')";
        uidList = dao.find(hql);
		return uidList;
	}
	public static List getDepList(String start,String end){
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		String uid = CollectiontoString.ListToString(EUtils.getUidList(start,end)) ;
        String hql = "select distinct dpt from UUser u where u.id in ( " +uid + ")";
        List depList = dao.find(hql);
		return depList;
	}
	
	public static List getZzUser(String start,String end){
		if(dao==null){
	        dao = (BaseDAOImpl) EUtils.getCtx().getBean("BaseDAOImpl");
		}
		String uid = CollectiontoString.ListToString(EUtils.getUidList(start,end)) ;
        String hql = " from UUser u where u.wkState='在职' and id in ( " +uid + ")";
        List zzList = dao.find(hql);
		return zzList;
	}
   //年龄段统计辅助方法  获取用户年龄
	@SuppressWarnings("deprecation")
	public static int getAge(int id){
		dao = (BaseDAOImpl) ctx.getBean("BaseDAOImpl");
		String hql = "from UUser u where u.id="+id;
		List user = dao.find(hql);
		UUser u = (UUser) user.get(0);
		Date birth = u.getBirth();
        Calendar calendar = Calendar.getInstance();
        int curyear = calendar.get(Calendar.YEAR);
        calendar.setTime(birth);
		int year = calendar.get(Calendar.YEAR);
        return curyear - year;
	}
	
	

}

